package uni.UNIBEE7B8D.view.User;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import uni.UNIBEE7B8D.BaseActivity;
import uni.UNIBEE7B8D.Enty.Result;
import uni.UNIBEE7B8D.Presenter.LoginPersenter;
import uni.UNIBEE7B8D.R;
import uni.UNIBEE7B8D.view.User.LoginUserPwd.LoginUserPwdActivity;

public class LoginActivity extends BaseActivity implements View.OnClickListener,LoginContract.LoginView {

    @BindView(R.id.guanbi)
    ImageView guanbi;
    @BindView(R.id.loginText)
    TextView loginText;
    @BindView(R.id.loginText1)
    TextView loginText1;
    @BindView(R.id.userName)
    EditText userName;
    @BindView(R.id.userPwd)
    EditText userPwd;
    @BindView(R.id.pwdView)
    LinearLayout pwdView;
    @BindView(R.id.mima_tishi)
    TextView mimaTishi;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.sendcode)
    TextView sendcode;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private LoginPersenter loginPersenter = new LoginPersenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        sharedPreferences = getSharedPreferences("kangconfig", Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        mimaTishi.setOnClickListener(this);
        login.setOnClickListener(this);
        sendcode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mima_tishi:
                Intent intent = new Intent(getBaseContext(), LoginUserPwdActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.login:

                loginPersenter.login(userName.getText().toString().trim(),userPwd.getText().toString().trim());
                break;
            case R.id.sendcode:
                loginPersenter.sendcode(userName.getText().toString().trim());
                break;
        }
    }

    @OnClick(R.id.guanbi)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void Toast(String s) {
        Toast.makeText(getBaseContext(),s,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void login(Result.ResultBean object) {
        Log.d("==",new Gson().toJson(object));
        editor.putString("userMessage", new Gson().toJson(object));
        editor.putString("access_token",object.getToken() );
        editor.commit();
        finish();
    }


}
